package net.contrend.material.dao;

import java.sql.Types;
import java.util.List;
import java.util.UUID;

import net.contrend.material.form.MaterialTypeForm;
import net.contrend.material.mapper.MaterialTypeMapper;
import net.contrend.system.dao.BaseDao;
import net.contrend.util.SqlUtils;
import net.contrend.util.StringUtils;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * <p>Copyright @ 2009 www.contrend.net</p>
 * <p>All right reserved. </p>
 * <p>项目名称			： 进销存管理系统 </p>
 * <p>JDK使用版本号		： jdk6.0 </P>
 * <p>描述				： 原料类别 DAO </p>
 * @版本					： 1.0.0 
 * @author				： 谢庚才
 *
 * <p>修改历史 </p>
 * <p>修改时间            修改人员    修改内容 </p>
 * <p>Dec 11, 2009       谢庚才     新建 </p>
 */
public class MaterialTypeDao extends BaseDao {

	private String table = "material_type";

	public MaterialTypeDao() {
	}
	
	public MaterialTypeDao(JdbcTemplate jt) {
        this.jt = jt;
    }
	
	public int insert(MaterialTypeForm materialType) {
		String columns = "mtId, code, name, createDate, status";
        String sql = SqlUtils.getInsertSql(this.table, columns);
        System.out.println(String.format("\n%1$s\n", sql));
        
        if (StringUtils.isBlank(materialType.getMtId())) {
        	materialType.setMtId(UUID.randomUUID().toString());
        }
        
        return this.jt.update(sql,
                new Object[]{
	        		materialType.getMtId(),
	        		materialType.getCode(),
	        		materialType.getName(),
	        		materialType.getCreateDate(),
	        		materialType.getStatus()
                },
                new int[]{
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.DATE,
                    Types.VARCHAR
                });
	}
	
	public int update(MaterialTypeForm materialType) {
		String columns = "code, name, createDate, status";
        String sql = SqlUtils.getUpdateSql(this.table, columns, "mtId = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql,
                new Object[]{
	        		materialType.getCode(),
	        		materialType.getName(),
	        		materialType.getCreateDate(),
	        		materialType.getStatus()
                },
                new int[]{
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.DATE,
                    Types.VARCHAR
                });
	}
	
	public int delete(String mtId) {
        String sql = SqlUtils.getDeleteSql(this.table, "mtId=?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql, new Object[]{mtId}, new int[]{Types.VARCHAR});
    }
	
	@SuppressWarnings("unchecked")
	public MaterialTypeForm queryById(String id) {
        String sql = "select * from "+this.table+" where mtId = ?";
        List<MaterialTypeForm> materialTypeList = this.jt.query(sql,
                new Object[]{id}, new int[]{Types.VARCHAR}, new MaterialTypeMapper());
        if (null != materialTypeList && 0 < materialTypeList.size()) {
            return materialTypeList.get(0);
        }
        return null;
    }
}
